package com.yunwushanhe.service.impl;

import ch.qos.logback.core.encoder.EchoEncoder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunwushanhe.bean.req.TagAddReq;
import com.yunwushanhe.bean.req.TagPageReq;
import com.yunwushanhe.bean.req.TagUpdateReq;
import com.yunwushanhe.bean.ret.ResponseResult;
import com.yunwushanhe.bean.vo.PageVO;
import com.yunwushanhe.bean.vo.TagListVO;
import com.yunwushanhe.bean.vo.TagPageVO;
import com.yunwushanhe.bean.vo.TagUpdateVO;
import com.yunwushanhe.constant.Constant;
import com.yunwushanhe.entity.Category;
import com.yunwushanhe.mapper.TagMapper;
import com.yunwushanhe.entity.Tag;
import com.yunwushanhe.service.TagService;
import com.yunwushanhe.util.BeanCopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author 抬头看看天
 * @since 2023-08-15 21:25:09
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Override
    public ResponseResult<PageVO<List<TagPageVO>>> getTagList(TagPageReq pageReq) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(pageReq.getName()),Tag::getName,pageReq.getName());
        queryWrapper.like(StringUtils.hasText(pageReq.getRemark()),Tag::getRemark,pageReq.getRemark());

        Page<Tag> tagPage = new Page<>();
        tagPage.setCurrent(pageReq.getPageNum());
        tagPage.setSize(pageReq.getPageSize());
        page(tagPage,queryWrapper);

        List<TagPageVO> tagPageVOS = BeanCopyUtil.copyBeanList(tagPage.getRecords(), TagPageVO.class);

        PageVO<List<TagPageVO>> pageVO = new PageVO<>(tagPageVOS, tagPage.getTotal());
        return ResponseResult.ok(pageVO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> addTag(TagAddReq tagAddReq) {
        Tag tag = BeanCopyUtil.copyBean(tagAddReq, Tag.class);
        save(tag);
        return ResponseResult.ok();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseResult<Object> deleteTagById(String id) {
        String[] arrayId = id.split(",");
        ArrayList<Long> listId = new ArrayList<>();
        for (String s : arrayId) {
            listId.add(Long.valueOf(s));
        }
        removeByIds(listId);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<TagUpdateVO> getTagById(Long id) {
        Tag tag = getById(id);
        TagUpdateVO tagUpdateVO = BeanCopyUtil.copyBean(tag, TagUpdateVO.class);
        return ResponseResult.ok(tagUpdateVO);
    }

    @Override
    public ResponseResult<Object> updateTagById(TagUpdateReq tagUpdateReq) {
        UpdateWrapper<Tag> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",tagUpdateReq.getId());
        updateWrapper.set("name",tagUpdateReq.getName());
        updateWrapper.set("remark",tagUpdateReq.getRemark());
        update(updateWrapper);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult<List<TagListVO>> listAllTag() {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getStatus, Constant.TAG_STATUS_NORMAL);
        List<Tag> tagList = list();
        List<TagListVO> tagListVOS = BeanCopyUtil.copyBeanList(tagList, TagListVO.class);
        return ResponseResult.ok(tagListVOS);
    }
}

